package com.wsj.a.collapsingdemo;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.CharacterStyle;
import android.text.style.ForegroundColorSpan;

/**
 * Created by linxiujie on 2018/8/3.
 */

public class StringUtils {

    /**
     * text部分带颜色
     *
     * @param content   完整字段
     * @param colorText 要替换颜色的字段
     * @param color     替换的颜色
     * @return 替换后的完整字段
     */
    public static SpannableStringBuilder getSpannableString(String content, String colorText, int color) {
        SpannableStringBuilder builder = new SpannableStringBuilder(content);
        int beginIndex = content.indexOf(colorText);
        int endIndex = beginIndex + colorText.length();
        if (beginIndex >= 0) {
            CharacterStyle characterStyle = new ForegroundColorSpan(color);
            builder.setSpan(characterStyle, beginIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return builder;
    }

    //印象金币页面 金币信息描述
    public static Spannable getColorKeyString(String content, String key, int textSize, int color) {

        int beginIndex = content.indexOf(key);
        Spannable span = new SpannableString(content);
        span.setSpan(new AbsoluteSizeSpan(textSize), beginIndex, beginIndex + key.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        span.setSpan(new ForegroundColorSpan(color), beginIndex, beginIndex + key.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return span;
    }

}
